package com.dq.service.statistics.impl;

import com.dq.controller.in.ProductSaleQuery;
import com.dq.repository.BaseRepositories;
import com.dq.service.org.OrgService;
import com.dq.service.statistics.ProductSaleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigDecimal;

/**
 * @Author luchangtong
 * @Date 2020/3/23 15:24
 */
@Service
public class ProductSaleServiceImpl extends BaseRepositories implements ProductSaleService {
    @Autowired
    private OrgService orgService;
    @PersistenceContext
    private EntityManager entityManager;
    @Override
    public BigDecimal countSumPrice(ProductSaleQuery productSaleQuery) {
        StringBuilder hql = new StringBuilder("select COALESCE(sum(u.salePrice),0) from ProductSaleStatistics u where u.deleted=false");
        if(productSaleQuery.getOrgId() != null){
            hql.append(" and u.org.id=:orgId");
        }else {
            // 只显示负责门店信息
            String orgIdsIn = orgService.getIdListAllByUser();
            if(StringUtils.isNotBlank(orgIdsIn)) {
                hql.append(" and u.org.id in ").append(orgIdsIn);
            }
        }
        if(productSaleQuery.getProductId() != null){
            hql.append(" and u.product.id=:productId");
        }
        if(productSaleQuery.getBeginTime() != null){
            hql.append(" and u.createTime >=:beginTime");
        }
        if(productSaleQuery.getEndTime() != null){
            hql.append(" and u.createTime <=:endTime");
        }
        Query q = this.entityManager.createQuery(hql.toString());
        if(productSaleQuery.getProductId() != null){
            q.setParameter("productId",productSaleQuery.getProductId());
        }
        if(productSaleQuery.getBeginTime() != null){
            q.setParameter("beginTime",productSaleQuery.getBeginTime());
        }
        if(productSaleQuery.getEndTime() != null){
            q.setParameter("endTime",productSaleQuery.getBeginTime());
        }
        if(productSaleQuery.getOrgId() != null){
            q.setParameter("orgId",productSaleQuery.getOrgId());
        }
        return (BigDecimal) q.getSingleResult();
    }

    @Override
    public Long countSumNum(ProductSaleQuery productSaleQuery) {

        StringBuilder hql = new StringBuilder("select COALESCE(sum(u.number),0) from ProductSaleStatistics u where u.deleted=false");
        if(productSaleQuery.getOrgId() != null){
            hql.append(" and u.org.id=:orgId");
        }else {
            // 只显示负责门店信息
            String orgIdsIn = orgService.getIdListAllByUser();
            if(StringUtils.isNotBlank(orgIdsIn)) {
                hql.append(" and u.org.id in ").append(orgIdsIn);
            }
        }
        if(productSaleQuery.getProductId() != null){
            hql.append(" and u.product.id=:productId");
        }
        if(productSaleQuery.getBeginTime() != null){
            hql.append(" and u.createTime >=:beginTime");
        }
        if(productSaleQuery.getEndTime() != null){
            hql.append(" u.createTime <=:endTime");
        }
        Query q = this.entityManager.createQuery(hql.toString());
        if(productSaleQuery.getProductId() != null){
            q.setParameter("productId",productSaleQuery.getProductId());
        }
        if(productSaleQuery.getBeginTime() != null){
            q.setParameter("beginTime",productSaleQuery.getBeginTime());
        }
        if(productSaleQuery.getEndTime() != null){
            q.setParameter("endTime",productSaleQuery.getBeginTime());
        }
        if(productSaleQuery.getOrgId() != null){
            q.setParameter("orgId",productSaleQuery.getOrgId());
        }
        return (Long) q.getSingleResult();
    }
}
